package com.woniu.controller.mallSystem;

import com.alibaba.excel.EasyExcel;
import com.woniu.model.gh.common.Result;
import com.woniu.model.mallSystem.vo.OrderItemVo;
import com.woniu.model.mallSystem.vo.OrderQueryVo;
import com.woniu.model.mallSystem.vo.OrderVo;
import com.woniu.model.mallSystem.vo.PageVo;
import com.woniu.service.mallSystem.IOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lzj
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @Autowired
    private RedisTemplate redisTemplate;



    /**
     * 条件搜索查询订单分页
     */
    @GetMapping("/findByCondition")
    public Result<PageVo> findByCondition(@RequestBody OrderQueryVo orderQueryVo) {
        PageVo page = orderService.advancedQuery(orderQueryVo.getPageNum(),orderQueryVo.getPageSize(), orderQueryVo);
        return Result.ok(page);
    }

    /**
     * 查询全部的分页
     * */
    @GetMapping("/findAll")
    @ApiOperation("查询所有订单分页")
    @Cacheable(cacheNames = "all_order_list")
    public Result findAllOrder(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "5") Integer pageSize) {
        PageVo page = orderService.findAllOrder(pageNum, pageSize);
        return Result.ok(page);
    }

    /**订单详情
     * 此订单下有若干个OrderItem，应该有名称图片数量小计
     * 被传到前端
     * 传入一个orderId，返回一个List
     * */
    @GetMapping("/getItemByOrderId/{id}")
    @ApiOperation("订单详情")
    public List<OrderItemVo> getItemByOrderId(@PathVariable Long id) {
        return orderService.findItemByOrderId(id);
    }



    /**
     * 导出数据Excel
     */
    @GetMapping("/export")
    @ApiOperation("导出订单")
    public void exportData(HttpServletResponse response) throws IOException {

        //1.调用业务层获取要导出数据
        List<OrderVo> list = orderService.findAll();
        //2.设置响应头，指定下载文件
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //文件名不使用汉字，否则需要进行编码，文件名会有乱码。告诉浏览器，这个文件要下载，并且指定文件名
        // 设置工作表名字,获取当前时间并格式化为字符串
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String sheetName = now.format(formatter)+"-order";

        response.setHeader("content-disposition","attachment;filename=" + sheetName + ".xlsx");
        //3.调用EasyExcel的方法导出文件
        //获取响应输出流
        ServletOutputStream out = response.getOutputStream();
        //设置工作表名字

        //参数：输出流，实体类
        EasyExcel.write(out,OrderVo.class)
                .sheet("订单表")   //工作簿名字
                .doWrite(list);  //要写出的数据列表
    }
}
